
import 'package:blue_plugin/bean/dev_find.dart';
import 'package:blue_plugin/bean/dev_info.dart';
import 'package:blue_plugin/bean/dev_state.dart';
import 'package:blue_plugin/blue_elves.dart';
import 'package:blue_plugin/utils/toast_util.dart';
import 'package:blue_plugin_example/base/base_blue_controller.dart';
import 'package:blue_plugin_example/bean/dev_info_init.dart';
import 'package:blue_plugin_example/res/colors.dart';

class PlugRouteController extends BaseBlueController {
  String ssy = "-";
  String szy = "-";
  String xl = "-";
  String ssyEdit = "";
  String szyEdit = "";
  String xlEdit = "";
  var btnColor = ColorStyle.color_btn_bg_2;
  String connectMsg = "连接";

  @override
  void onInit() {
    super.onInit();
    baseGetTime();
  }

  getBlueElves(){
    return BlueElves.instance;
  }

  ///1.扫描设备
  onConnectBtn() {
    clearEditInfo();
    String uuid = DevInfoInit.DEV_XUEYA_UUID;
    String devName = DevInfoInit.DEV_XUEYA_NAME;
    List<String> _charaList = [];
    _charaList.insert(0, DevInfoInit.DEV_XUEYA_TZ_2);
    _charaList.insert(1, DevInfoInit.DEV_XUEYA_TZ_1);
    devInfo = DevInfo(DevInfoInit.DEV_TYPE_XUEYA, uuid, _charaList, devName, DEVFind.UUID, DevInfoInit.DEV_XUEYA_MARK);
    baseGetTime();
    baseOnConnectBtn(devInfo!);
  }

  ///2.设置按钮的状态值
  @override
  baseSetConnectMsg(DevState state, String error, String errorMsg) {
    print("baseSetConnectMsg$state--$error--$errorMsg");
    connectState = state;
    if (errorMsg.isNotEmpty) {
      ToastUtils.show(errorMsg);
    }
    if (state == DevState.CONNECT) {
      connectMsg = "连接";
    }

    if (state == DevState.CONNECTING) {
      connectMsg = "连接中";
    }

    if (state == DevState.RECONNECT) {
      connectMsg = "重新连接";
    }

    if (state == DevState.COLLECTIONING) {
      connectMsg = "采集中";
    }

    if (state == DevState.RECAPTURE) {
      connectMsg = "重新采集";
    }
    update();
  }

  ///3.根据设备类型 子类做处理
  @override
  devValueOpt(isSuccess, data,strdata, uuid) {
    if (isSuccess) {    //成功
      print("收到的测量数据:${data}");
      if (devInfo!.devType == DevInfoInit.DEV_TYPE_XUEYA) {
        if (uuid == DevInfoInit.DEV_XUEYA_TZ_1) {
          baseSetConnectMsg(DevState.RECAPTURE, "4", "");
          setHei(data[1]);
          setLow(data[3]);
          setXl(data[14]);
          setEdit(data[1], data[3], data[14]);
        }

        if (uuid == DevInfoInit.DEV_XUEYA_TZ_2) {
          setHei(data[1]);
        }
      }
    }
  }

  onUpDataBtn() {
    ToastUtils.showError("提交");
  }
  ///4 销毁
  @override
  destroyBlue(){
    super.destroyBlue();
  }

  setBtnbg(bool flag) {
    if (flag) {
      btnColor = ColorStyle.color_btn_bg;
    } else {
      btnColor = ColorStyle.color_btn_bg_2;
    }
    update();
  }

  setHei(int value) {
    ssy = value.toString();
    update();
  }

  setLow(int value) {
    szy = value.toString();
    update();
  }

  setXl(int value) {
    xl = value.toString();
    update();
  }

  setEdit(int value, int value2, int value3) {
    ssyEdit = value.toString();
    szyEdit = value2.toString();
    xlEdit = value3.toString();
    update();
  }

  void clearEditInfo() {
    ssy = "-";
    szy = "-";
    xl = "-";
    ssyEdit = "";
    szyEdit = "";
    xlEdit = "";
    update();
  }
}
